<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * 后台操作日志模型类
 * @author 66diqiu-li
 *
 */
class Admin_Adminlog_model extends CI_Model {
	
	/**
     * @var 表名称
     */
    private $table;

    public function __construct()
    {
        parent::__construct();
        $this->table  = $this->db->dbprefix("admin_log");
    }
	
    /**
	 * 记录后台操作日志
     * @param array $data $data=array('action'=>'add','log'=>'日志信息');
     * @return bool
	 */
    function record($data=array()){
    	
    	//操作类型
    	$operate = array(
    	    'add' => '添加',
    	    'edit' => '修改',
    	    'remove' => '移除',
    	    'install' => '安装',
    	    'unstall' => '卸载',
    	);
    	if(empty($data)) return false;
    	$object = array();
        $object['sid'] = $GLOBALS['sid'];
    	$object['add_time'] = date('Y-m-d H:i:s',now());
    	$object['ip'] = get_client_ip();
    	//后台操作员  session TODO
    	$object['action_user'] = $this->session->userdata('admin_user_name');
    	//$object['url'] = $this->uri->uri_string();//页面来源
    	$object['url'] = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER']  : '';
    	$object['content'] = $operate[$data['action']].'-'.$data['log'];
        //操作日志表
        return $this->db->insert($this->table, $object);
    }
    
	
	/**
	 * 
	 * 删除数据
	 * @param array $where 删除条件
	 */
	function remove_row($where=array()){
		if(!empty($where))$this->db->where($where, NULL, FALSE);
		return $this->db->delete($this->table);
	}
	
	/**
	 * 获取所有管理管理员登录日志
	 * @param int $page_size 每页显示数量
	 * @param int $now_page 第几页
	 * @param string $where 选择条件
     * @return array
	 */
	function get_list($page_size=20,$now_page=1,$where = ''){
        $this->db->select('id,action_user,ip,content,url,add_time');
        $this->db->order_by('add_time','DESC');
        if(!empty($where)){
        	$this->db->where($where, NULL, FALSE);
        }
        $query = $this->db->get($this->table,$page_size,($now_page-1)*$page_size);
        $data = array();
        foreach($query->result_array() as $row){
            $data[] = $row;
        }        
        return $data;
	}
	
	/**
	 * 
	 * 删除一个月前登录日志
	 */
	function deleteAMonthago(){
        $this->db->where('sid',$GLOBALS['sid']);
		$this->db->where('add_time <=',date('Y-m-d H:i:s',now()-(60*60*24*30)));
		return $this->db->delete($this->table);
	}
	
	/**
	 * 
	 * 获取总数量
	 * @param array $where 查询条件
	 */
	function get_count($where=array()){
        $this->db->select('id');
        $this->db->from($this->table);
        if(!empty($where))$this->db->where($where, NULL, FALSE);        
        return $this->db->count_all_results();        
	}
}